package demo.jdbc.example;

import demo.jdbc.example.dao.UsersDao;
import demo.jdbc.example.domain.User;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet(name = "ServletServletInsertTest", value = "/ServletServletInsertTest")
public class ServletServletInsertTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UsersDao usersDao = new UsersDao();
        // 封装User信息
        User user = new User();
        user.setId(99);
        user.setUsername("yangjignyao");
        user.setPassword("123");
        user.setEmail("yjy@sina.com");
        user.setBirthday(new Date());

        // 调用DAO新增数据到数据库中
        boolean flag = usersDao.insert(user);
        // 输出结果
        PrintWriter out = response.getWriter();
        if (flag) {
            out.println("新增数据成功，2秒后跳转到所有用户数据列表");
            response.setHeader("refresh", "2;url=ServletFindAllUsersTest");
        } else {
            out.println("新增数据失败");
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
